package MTT::Libs::Menu;

use MooseX::Singleton;

use Data::Dumper;		
use Tie::IxHash;
use MTT::Libs::ObjectAccess;

use utf8;


sub get_menu {
	my ($self, $c) = @_;

	$self->load_menu($c) unless exists $self->{menu}->{ $c->user->id };
	return $self->{menu}->{ $c->user->id };
}

sub load_menu {
	my ($self, $c) = @_;

	my $objects = MTT::Libs::ObjectAccess->instance;

	my %types;
   	foreach my $type ( $c->model('DBIC::SwitchType')->all() ) {
		next unless $c->check_user_roles('admin') || $objects->check_objects_access($c, 'Оборудование', $type->id);

		my $switches = $c->model('DBIC::Switch')->search({id_type => $type->id});

		my $hw_type = {};
		while ( my $switch = $switches->next() ) {
			$hw_type->{ $switch->host_code } = $c->uri_for('/reports', $type->id, $switch->id);
		}
		next unless keys %$hw_type;
		
		$hw_type->{' Все типы'} = $c->uri_for('/reports', $type->id);
#		$types{ $type->type }->{' все типы'} = $c->uri_for('/reports/id', $type->id);
		$types{ $type->type } = $hw_type;
	}

	my %reports;
#	tie my \%menu, 'Tie::IxHash';
	tie %reports, 'Tie::IxHash', (
			'Все оборудование' => $c->uri_for('/reports/'),
			%types,
			'Шаблоны' => $c->uri_for('/reports/templates')
		);

	my %menu;
	tie %menu, 'Tie::IxHash', (
		'Отчеты'  => \%reports,
		'Профиль' => $c->uri_for('/users/id', $c->user->id),
	);

	if ( $c->check_user_roles('admin') ) {
		$menu{'Алармы'}  = $c->uri_for('/alarms');
		$menu{'Пользователи'} = {
			'Список'  => $c->uri_for('/users/list'),
			'Создать' => $c->uri_for('/users/register'),
			'Доступ'  => $c->uri_for('/users/access'),
		};
	}

	$menu{'Выход'} = $c->uri_for('/logout');

	$self->{menu}->{ $c->user->id } = \%menu;
}

__PACKAGE__->meta->make_immutable;
1;
